﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Detail.DeepProcessing;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.DeepProcessing;

namespace Traceability.App.Api.Application.Handler.Detail.DeepProcessing
{
    public class AddVendorHandler : IRequestHandler<AddVendorCommand, ApiResult<int>>
    {
        IBaseRepository<DeepVendorModel> deepvenRepo;
        IMapper mapper;

        public AddVendorHandler(IBaseRepository<DeepVendorModel> deepvenRepo, IMapper mapper)
        {
            this.deepvenRepo = deepvenRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(AddVendorCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var model = mapper.Map<DeepVendorModel>(request);
            int a = deepvenRepo.Add(model).Result;
            if (a > 0)
            {
                res.Msg = "保存成功";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "保存失败";
                res.Code = ApiEnum.Fail;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
